package com.app.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.app.common.IdCardNumberMethod;
import com.app.common.SystemConstant;
import com.app.model.*;
import com.app.service.*;
import com.app.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.web.multipart.MultipartFile;

@RestController
@Controller
@RequestMapping("/api/excel")
public class TextController {

    @Autowired
    PersonService personService;
    @Autowired
    PersonPositionService personPositionService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    PersonResumeService personResumeService;
    @Autowired
    PersonCheckService personCheckService;
    @Autowired
    PersonTrainService personTrainService;
    @Autowired
    PersonFamilyService personFamilyService;
    @Autowired
    ElectronicService electronicService;
    @Autowired
    CategoryPhoneService categoryPhoneService;
    @Autowired
    ElectronicMediaService electronicMediaService;
    @Autowired
    TestsService testsService;
    @Autowired
    TypeService typeService;
    @Autowired
    TestsPersonService testsPersonService;
    @Autowired
    PlantService plantService;
    @Autowired
    ProtectionPointService protectionPointService;
    @Autowired
    private ComputerService computerService;
    public static final SimpleDateFormat DATA_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * excel导出 列表 指定模板
     *
     * @return
     */
    @PostMapping("/exportExcel")//个人信息导出
    public void exportExcel(HttpServletResponse response, @RequestBody JSONObject json) throws IOException {
//        try {
        Integer personId = Integer.valueOf(json.get("personId").toString());
        Map<String, Object> map = new HashMap<>();
        PersonModel model = personService.selectById(personId);
        List<PersonPositionModel> positionModelList = personPositionService.listExport(personId);//职务信息
        for (int i = 0; i < positionModelList.size(); i++) {
            DepartmentModel departmentModel = departmentService.getDepByDepId(positionModelList.get(i).getDepartmentId());
            positionModelList.get(i).setDepName(departmentModel.getName());
            if (positionModelList.get(i).getIsMajor() != null && positionModelList.get(i).getIsMajor() == 1) {
                positionModelList.get(i).setIsMajorText("是");
            } else {
                positionModelList.get(i).setIsMajorText("否");
            }
        }
        List<PersonResumeModel> resumeModelList = personResumeService.listExport(personId);//简历信息
        List<PersonFamilyModel> familyModelList = personFamilyService.listExport(personId);//家庭成员信息采集表
        for (int i = 0; i < familyModelList.size(); i++) {
            CategoryModel categoryModel = categoryService.getCategoryById(familyModelList.get(i).getAppellation());
            familyModelList.get(i).setAppellationText(categoryModel.getName());
            CategoryModel categoryModel2 = categoryService.getCategoryById(familyModelList.get(i).getZzmm());
            familyModelList.get(i).setZzmmText(categoryModel2.getName());
        }

        if (model.getFaceUrl() != null && model.getFaceUrl() == null) {
            model.setFaceUrl("暂无头像");
        }
        if (model.getPersonType() != null) {
            CategoryModel categoryModel2 = categoryService.getCategoryById(model.getPersonType());
            if (categoryModel2 != null) {
                model.setPersonTypeText(categoryModel2.getName());
            }
        }
        List<ElectronicModel> modelList1 = electronicService.selectListByPersonId(model.getId(), 57);//取手机
        if (modelList1 != null && modelList1.size() > 0) {
            for (int i = 0; i < modelList1.size(); i++) {
                CategoryPhoneModel categoryPhoneModel = categoryPhoneService.getCategoryPhoneById(modelList1.get(i).getBrand());
                CategoryPhoneModel categoryPhoneModel1 = categoryPhoneService.getCategoryPhoneById(modelList1.get(i).getDevice());
                modelList1.get(i).setBrandText(categoryPhoneModel.getName());
                modelList1.get(i).setDeviceText(categoryPhoneModel1.getName());
                CategoryModel device = categoryService.getCategoryById(modelList1.get(i).getDeviceType());//设备类型
                modelList1.get(i).setDeviceTypeText(device.getName());
            }
        }
        List<ElectronicModel> modelList2 = electronicService.selectListByPersonId(model.getId(), 58);//取手表
        if (modelList2 != null && modelList2.size() > 0) {
            for (int i = 0; i < modelList2.size(); i++) {
                CategoryPhoneModel categoryPhoneModel = categoryPhoneService.getCategoryPhoneById(modelList2.get(i).getBrand());
                CategoryPhoneModel categoryPhoneModel1 = categoryPhoneService.getCategoryPhoneById(modelList2.get(i).getDevice());
                modelList2.get(i).setBrandText(categoryPhoneModel.getName());
                modelList2.get(i).setDeviceText(categoryPhoneModel1.getName());
                CategoryModel device = categoryService.getCategoryById(modelList2.get(i).getDeviceType());//设备类型
                modelList2.get(i).setDeviceTypeText(device.getName());
            }
        }
        List<ElectronicModel> modelList3 = electronicService.selectListByPersonId(model.getId(), 59);//取手环
        if (modelList3 != null && modelList3.size() > 0) {
            for (int i = 0; i < modelList3.size(); i++) {
                CategoryPhoneModel categoryPhoneModel = categoryPhoneService.getCategoryPhoneById(modelList3.get(i).getBrand());
                CategoryPhoneModel categoryPhoneModel1 = categoryPhoneService.getCategoryPhoneById(modelList3.get(i).getDevice());
                modelList3.get(i).setBrandText(categoryPhoneModel.getName());
                modelList3.get(i).setDeviceText(categoryPhoneModel1.getName());
                CategoryModel device = categoryService.getCategoryById(modelList3.get(i).getDeviceType());//设备类型
                modelList3.get(i).setDeviceTypeText(device.getName());
            }
        }
        List<ElectronicModel> modelList4 = electronicService.selectListByPersonId(model.getId(), 88);//取眼镜
        if (modelList4 != null && modelList4.size() > 0) {
            for (int i = 0; i < modelList4.size(); i++) {
                CategoryPhoneModel categoryPhoneModel = categoryPhoneService.getCategoryPhoneById(modelList4.get(i).getBrand());
                CategoryPhoneModel categoryPhoneModel1 = categoryPhoneService.getCategoryPhoneById(modelList4.get(i).getDevice());
                modelList4.get(i).setBrandText(categoryPhoneModel.getName());
                modelList4.get(i).setDeviceText(categoryPhoneModel1.getName());
                CategoryModel device = categoryService.getCategoryById(modelList4.get(i).getDeviceType());//设备类型
                modelList4.get(i).setDeviceTypeText(device.getName());
            }
        }
        ElectronicMediaModel mediaModel = electronicMediaService.getElcMediaByPersonId(model.getId(), 63);
        if (mediaModel != null) {
            model.setMediaWX(mediaModel.getMediaNumber());
        }
        ElectronicMediaModel mediaModel2 = electronicMediaService.getElcMediaByPersonId(model.getId(), 64);
        if (mediaModel2 != null) {
            model.setMediaQQ(mediaModel2.getMediaNumber());
        }
        ElectronicMediaModel mediaModel3 = electronicMediaService.getElcMediaByPersonId(model.getId(), 68);
        if (mediaModel3 != null) {
            model.setMediaWB(mediaModel3.getMediaNumber());
        }
        List<ElectronicModel> modelList = new ArrayList<>();
        if (modelList1 != null) {
            modelList.addAll(modelList1);
        }
        if (modelList2 != null) {
            modelList.addAll(modelList2);
        }
        if (modelList3 != null) {
            modelList.addAll(modelList3);
        }
        if (modelList4 != null) {
            modelList.addAll(modelList4);
        }

        List<PersonTrainModel> trainModelList = personTrainService.getTrainList(personId, null);
        if (trainModelList.size() > 0) {
            List<PersonTrainModel> list = new ArrayList<>();
            for (int i = 0; i < trainModelList.size(); i++) {
                if (trainModelList.get(i).getTrainDesc() != null) {
                    CategoryModel model1 = categoryService.getCategoryById(trainModelList.get(i).getTrainDesc());
                    if (model1 != null) {
                        trainModelList.get(i).setTrainDesc(model1.getId());
                        trainModelList.get(i).setTrainTotal(model1.getName());//训练总成绩
                    }
                }
                String trainResult = trainModelList.get(i).getTrainResult();
                list = JSON.parseObject(trainResult, new TypeReference<List<PersonTrainModel>>() {
                });
                trainModelList.get(i).setId(i + 1);
                trainModelList.get(i).setPositionDesc(model.getPositionDesc());
                trainModelList.get(i).setName(model.getName());
                trainModelList.get(i).setGenderText(model.getGender());
                trainModelList.get(i).setAge(IdCardNumberMethod.getAgeForIdCard(model.getIdcard()));
            }
            for (int i = 0; i < trainModelList.size(); i++) {
                for (int i1 = 0; i1 < list.size(); i1++) {
                    if (list.get(i1).getTrainObject() == 54) {//军事体育
                        trainModelList.get(i).setPhysicalScore(list.get(i1).getTrainScore());
                        trainModelList.get(i).setPhysicalLevel(list.get(i1).getTrainLevelText());
                    }
                    if (list.get(i1).getTrainObject() == 55) {//军事教育
                        trainModelList.get(i).setEducationLevel(list.get(i1).getTrainLevelText());
                    }
                    if (list.get(i1).getTrainObject() == 83) {//理论考核
                        trainModelList.get(i).setTheoreticalScore(list.get(i1).getTrainScore());
                        trainModelList.get(i).setTheoreticalLevel(list.get(i1).getTrainLevelText());
                    }
                    if (list.get(i1).getTrainObject() == 107) {//轻武器操作
                        trainModelList.get(i).setWeaponScore(list.get(i1).getTrainScore());
                        trainModelList.get(i).setWeaponLevel(list.get(i1).getTrainLevelText());
                    }
                    if (list.get(i1).getTrainObject() == 108) {//专业岗位考核
                        trainModelList.get(i).setPostScore(list.get(i1).getTrainScore());
                        trainModelList.get(i).setPostLevel(list.get(i1).getTrainLevelText());
                    }
                }
            }
        }
        List<ElectronicMediaModel> electronicMediaModels = electronicMediaService.selectElcMediaByPersonId(personId);
        for (int i = 0; i < electronicMediaModels.size(); i++) {
            CategoryModel categoryById = categoryService.getCategoryById(electronicMediaModels.get(i).getMediaType());
            electronicMediaModels.get(i).setMediaTypeText(categoryById.getName());
            if (electronicMediaModels.get(i).getIllegal() == 0) {
                electronicMediaModels.get(i).setIllegalText("否");
            }
            if (electronicMediaModels.get(i).getIllegal() == 1) {
                electronicMediaModels.get(i).setIllegalText("是");
            }
        }
//            model.setUrl(new URL(SystemConstant.DEFAULT_PDF_SERVICE_URL + "gvXTpTTX.png"));
        map.put("data", model);
        map.put("resumeModelList", resumeModelList);//简历信息sheet
        map.put("familyModelList", familyModelList);//家庭信息sheet
        map.put("positionModelList", positionModelList);//职务信息sheet
        map.put("list", modelList);//电子设备信息sheet
        map.put("trainModelList", trainModelList);//军事训练信息sheet
        map.put("electronicMedia", electronicMediaModels);//社交媒体账号
        TemplateExportParams params = new TemplateExportParams(SystemConstant.PersonalExportURL, true);
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
        String fileName = model.getName() + "" + time;
        exportExcel(params, map, fileName, response);
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }
    }


    /**
     * 办公信息化设备批量导出
     */
    @PostMapping("/computerExcel")
    public void computerExcel(
            @RequestBody JSONObject json,
            HttpServletResponse response) {
        String ids = json.get("ids").toString();
        Integer type = (Integer) json.get("type");
        try {
            Map<String, Object> map = new HashMap<>();
            String[] items = ids.split(",");
            List<Integer> appIdList = Stream.of(items).map(Integer::parseInt).collect(Collectors.toList());
            //获取列表
            List<ComputerModel> plantModelList = computerService.getPlantByIds(appIdList);
            for (int i = 0; i < plantModelList.size(); i++) {
                DepartmentModel depByDepId = departmentService.getDepByDepId(plantModelList.get(i).getDepartmentId());
                plantModelList.get(i).setDepartmentText(depByDepId.getName());
            }
            PersonModel model = new PersonModel();
            if (type == 1) {
                map.put("list", plantModelList);
                model.setName("笔记本电脑使用情况统计");
                map.put("model", model);
                TemplateExportParams params = new TemplateExportParams(SystemConstant.ElectronicExportURL, true);
                String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
                String fileName = "笔记本电脑使用情况统计" + "" + time;
                exportExcel(params, map, fileName, response);
            }
            if (type == 2) {
                map.put("list", plantModelList);
                model.setName("业务单机电脑使用情况统计");
                map.put("model", model);
                TemplateExportParams params = new TemplateExportParams(SystemConstant.SingleExportURL, true);
                String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
                String fileName = "业务单机电脑使用情况统计" + "" + time;
                exportExcel(params, map, fileName, response);
            }
            if (type == 3) {
                map.put("list", plantModelList);
                model.setName("移动硬盘使用情况统计");
                map.put("model", model);
                TemplateExportParams params = new TemplateExportParams(SystemConstant.EquipExportURL, true);
                String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
                String fileName = "移动硬盘使用情况统计" + "" + time;
                exportExcel(params, map, fileName, response);
            }
            if (type == 4) {
                map.put("list", plantModelList);
                model.setName("科研电脑终端使用情况统计");
                map.put("model", model);
                TemplateExportParams params = new TemplateExportParams(SystemConstant.OfficeExportURL, true);
                String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
                String fileName = "科研电脑终端使用情况统计" + "" + time;
                exportExcel(params, map, fileName, response);
            }
            if (type == 5) {
                map.put("list", plantModelList);
                model.setName("其他设备使用情况统计");
                map.put("model", model);
                TemplateExportParams params = new TemplateExportParams(SystemConstant.OtherExportURL, true);
                String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
                String fileName = "其他设备使用情况统计" + "" + time;
                exportExcel(params, map, fileName, response);
            }
            if (type == 6) {
                map.put("list", plantModelList);
                model.setName("办公电脑使用情况统计");
                map.put("model", model);
                TemplateExportParams params = new TemplateExportParams(SystemConstant.OfficeComputerExportURL, true);
                String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
                String fileName = "办公电脑使用情况统计" + "" + time;
                exportExcel(params, map, fileName, response);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    public static void exportExcel(TemplateExportParams params, Map<String, Object> map, String fileName, HttpServletResponse response) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        downLoadExcel(fileName, response, workbook);
    }

    private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
            ServletOutputStream outStream = null;
            try {
                outStream = response.getOutputStream();
                workbook.write(outStream);

            } finally {
                outStream.flush();
                outStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 指定表格位置上传
     *
     * @param "file"
     * @return
     * @throws Exception
     */
    @PostMapping("/testImport")
    public String testImport(
            HttpServletRequest request
    ) throws Exception {
        // 获取文件
        List<MultipartFile> file = ((MultipartHttpServletRequest) request).getFiles("file");

        File fileEx = update(file.get(0));
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(fileEx));
        int tabIndex = 0;
        Sheet sheet = xssfWorkbook.getSheetAt(tabIndex);
        Row row = null;
        Cell cell_a0 = null;
        row = sheet.getRow(1);     //指定行
        cell_a0 = row.getCell(1);  //制定列
        cell_a0.setCellType(CellType.STRING);
        String cellValue0 = cell_a0.getStringCellValue();//姓名
        Cell cell_a1 = null;
        row = sheet.getRow(2);     //指定行
        cell_a1 = row.getCell(1);  //制定列
        cell_a1.setCellType(CellType.STRING);
        String cellValue1 = cell_a1.getStringCellValue();//民族
        Cell cell_a2 = null;
        row = sheet.getRow(3);     //指定行
        cell_a2 = row.getCell(1);  //制定列
        cell_a2.setCellType(CellType.STRING);
        String cellValue2 = cell_a2.getStringCellValue();//入党时间
        Cell cell_a3 = null;
        row = sheet.getRow(4);     //指定行
        cell_a3 = row.getCell(1);  //制定列
        cell_a3.setCellType(CellType.STRING);
        String cellValue3 = cell_a3.getStringCellValue();//学历
        Cell cell_a4 = null;
        row = sheet.getRow(5);     //指定行
        cell_a4 = row.getCell(1);  //制定列
        cell_a4.setCellType(CellType.STRING);
        String cellValue4 = cell_a4.getStringCellValue();//人员类型
        Cell cell_a5 = null;
        row = sheet.getRow(6);     //指定行
        cell_a5 = row.getCell(1);  //制定列
        cell_a5.setCellType(CellType.STRING);
        String cellValue5 = cell_a5.getStringCellValue();//衔级
        Cell cell_a6 = null;
        row = sheet.getRow(7);     //指定行
        cell_a6 = row.getCell(1);  //制定列
        cell_a6.setCellType(CellType.STRING);
        String cellValue6 = cell_a6.getStringCellValue();//联系电话
        Cell cell_a7 = null;
        row = sheet.getRow(8);     //指定行
        cell_a7 = row.getCell(1);  //制定列
        cell_a7.setCellType(CellType.STRING);
        String cellValue7 = cell_a7.getStringCellValue();//详细住址
        Cell cell_a8 = null;
        row = sheet.getRow(9);     //指定行
        cell_a8 = row.getCell(1);  //制定列
        cell_a8.setCellType(CellType.STRING);
        String cellValue8 = cell_a8.getStringCellValue();//现任单位及职务
        Cell cell_a11 = null;
        row = sheet.getRow(1);     //指定行
        cell_a11 = row.getCell(3);  //制定列
        cell_a11.setCellType(CellType.STRING);
        String cellValue11 = cell_a11.getStringCellValue();//性别
        Cell cell_a12 = null;
        row = sheet.getRow(2);     //指定行
        cell_a12 = row.getCell(3);  //制定列
        cell_a12.setCellType(CellType.STRING);
        String cellValue12 = cell_a12.getStringCellValue();//籍贯
        Cell cell_a13 = null;
        row = sheet.getRow(3);     //指定行
        cell_a13 = row.getCell(3);  //制定列
        cell_a13.setCellType(CellType.STRING);
        String cellValue13 = cell_a13.getStringCellValue();//参加工作时间
        Cell cell_a14 = null;
        row = sheet.getRow(4);     //指定行
        cell_a14 = row.getCell(3);  //制定列
        cell_a14.setCellType(CellType.STRING);
        String cellValue14 = cell_a14.getStringCellValue();//所学专业
        Cell cell_a15 = null;
        row = sheet.getRow(5);     //指定行
        cell_a15 = row.getCell(3);  //制定列
        cell_a15.setCellType(CellType.STRING);
        String cellValue15 = cell_a15.getStringCellValue();//入伍时间
        Cell cell_a16 = null;
        row = sheet.getRow(6);     //指定行
        cell_a16 = row.getCell(3);  //制定列
        cell_a16.setCellType(CellType.STRING);
        String cellValue16 = cell_a16.getStringCellValue();//专业技术职务
        Cell cell_a17 = null;
        row = sheet.getRow(7);     //指定行
        cell_a17 = row.getCell(3);  //制定列
        cell_a17.setCellType(CellType.STRING);
        String cellValue17 = cell_a17.getStringCellValue();//身份证号
        Cell cell_a21 = null;
        row = sheet.getRow(1);     //指定行
        cell_a21 = row.getCell(5);  //制定列
        cell_a21.setCellType(CellType.STRING);
        String cellValue21 = cell_a21.getStringCellValue();//出生日期
        Cell cell_a22 = null;
        row = sheet.getRow(2);     //指定行
        cell_a22 = row.getCell(5);  //制定列
        cell_a22.setCellType(CellType.STRING);
        String cellValue22 = cell_a22.getStringCellValue();//出生地
        Cell cell_a23 = null;
        row = sheet.getRow(3);     //指定行
        cell_a23 = row.getCell(5);  //制定列
        cell_a23.setCellType(CellType.STRING);
        String cellValue23 = cell_a23.getStringCellValue();//健康状况
        Cell cell_a24 = null;
        row = sheet.getRow(4);     //指定行
        cell_a24 = row.getCell(5);  //制定列
        cell_a24.setCellType(CellType.STRING);
        String cellValue24 = cell_a24.getStringCellValue();//毕业院校
        Cell cell_a25 = null;
        row = sheet.getRow(5);     //指定行
        cell_a25 = row.getCell(5);  //制定列
        cell_a25.setCellType(CellType.STRING);
        String cellValue25 = cell_a25.getStringCellValue();//岗位层次
        Cell cell_a26 = null;
        row = sheet.getRow(6);     //指定行
        cell_a26 = row.getCell(5);  //制定列
        cell_a26.setCellType(CellType.STRING);
        String cellValue26 = cell_a26.getStringCellValue();//是否特殊人才
        Cell cell_a31 = null;
        row = sheet.getRow(1);     //指定行
        cell_a31 = row.getCell(6);  //制定列
        cell_a31.setCellType(CellType.STRING);
        String cellValue31 = cell_a31.getStringCellValue();//照片
        PersonModel personModel = new PersonModel();
        personModel.setName(cellValue0);
        personModel.setRdTime(cellValue2);
        CategoryModel categoryModel3 = categoryService.getPersonCate(cellValue4);
        personModel.setPersonType(categoryModel3.getId());
        personModel.setPhone(cellValue6);
        personModel.setAddress(cellValue7);
        personModel.setPositionDesc(cellValue8);
        personModel.setNativePlace(cellValue12);
        personModel.setJoinWorkTime(cellValue13);
        personModel.setMajor(cellValue14);
        personModel.setRwTime(cellValue15);
        personModel.setIdcard(cellValue17);
        personModel.setBirthday(cellValue21);
        personModel.setBirthPlace(cellValue22);
        personModel.setIsHealth(cellValue23);
        personModel.setSchool(cellValue24);
        personModel.setIsSpecial(cellValue26);
        personModel.setFaceUrl(cellValue31);
        personModel.setDepartmentId(2);
        personModel.setZzmm(30);
        personService.insert(personModel);
        fileEx.delete();
        return cellValue26;
    }


    private File update(MultipartFile file) throws IOException {
        String path = SystemConstant.DEFAULT_UPLOAD_URL;
        //获得上传文件名
        String fileName = file.getOriginalFilename();
        int lastIndexOf = fileName.lastIndexOf(".");
        String date = getRandomString2(8);
        //获取文件的后缀名 .jpg
        String suffix = fileName.substring(lastIndexOf);
        File filePath = new File(path + File.separator + date + suffix);
        System.out.println(filePath);
        //如果文件目录不存在，创建目录
        if (!filePath.getParentFile().exists()) {
            filePath.getParentFile().mkdirs();
        }
        file.transferTo(filePath);
        return filePath;
    }

    public static String getRandomString2(int length) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(3);
            long result = 0;
            switch (number) {
                case 0:
                    result = Math.round(Math.random() * 25 + 65);
                    sb.append(String.valueOf((char) result));
                    break;
                case 1:
                    result = Math.round(Math.random() * 25 + 97);
                    sb.append(String.valueOf((char) result));
                    break;
                case 2:
                    sb.append(String.valueOf(new Random().nextInt(10)));
                    break;
            }
        }
        return sb.toString();
    }

    /**
     * 按顺序读取表格上传
     *
     * @param file
     * @return
     * @throws Exception
     */

    @PostMapping("/testImport2")//sheet导入
    public Result<Object> testImport2(@RequestPart("file") MultipartFile file) throws Exception {
        importUsers(file);
        return Result.success("");
    }

    public Boolean importUsers(MultipartFile file) {

        ImportParams importParams = new ImportParams();
        //标题行设置为1行，默认是0，可以不设置；依实际情况设置。
        importParams.setTitleRows(0);
        // 表头设置为1行
        importParams.setHeadRows(1);
        importParams.setStartSheetIndex(1);
        try {
            //读取excel
            List<PersonPositionModel> users = ExcelImportUtil.importExcel(file.getInputStream(), PersonPositionModel.class, importParams);
            //批量插入
            boolean isSuccess = true;
            //返回结果
            return isSuccess;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


}